package com.sh1604.duitang.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sh1604.duitang.R;
import com.sh1604.duitang.activity.MyThemeActivity;
import com.sh1604.duitang.view.CircleImageView;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * Created by Double on 2016/7/20.
 */
public class MyPagerAdapter extends BaseAdapter{
    private List<HashMap<String,Object>> list;
    private Context context;
    private final int TYPE_MY_ONE = 0;
    private final int TYPE_MY_TWO = 1;
    private final int TYPE_MY_THREE = 2;
    private final int count = TYPE_MY_THREE+1;

    public MyPagerAdapter(Context context,List<HashMap<String,Object>> list1) {
        this.context=context;
         this.list=list1;
    }
    //多类型的需要重写的方法
    @Override
    public int getItemViewType(int position) {
        if (position==0){
            return  TYPE_MY_ONE;
        }
       else if (position==1){
            return  TYPE_MY_TWO;
        }
        return TYPE_MY_THREE;
    }


    @Override
    public int getViewTypeCount() {
        return count;
    }

    //找控件
//-----------------------------------------------------------------------------------------------------------
    class MyTheme {
        private ImageView myhead_iv;
        private CircleImageView myhead_cv;
        private TextView myhead_tv, myhead_tv2;
        private RelativeLayout  rl_theme;

        public MyTheme(View view) {
            myhead_iv = (ImageView) view.findViewById(R.id.my_item_head_iv);
            myhead_cv = (CircleImageView) view.findViewById(R.id.view);
            myhead_tv = (TextView) view.findViewById(R.id.my_item_head_tv);
            myhead_tv2 = (TextView) view.findViewById(R.id.my_item_head_tv2);
            rl_theme= (RelativeLayout) view.findViewById(R.id.rl_theme);
        }
    }
    class Mymiddle {
        private TextView mymiddle_tv, mymiddle_tv2, mymiddle_tv3;
        private ImageView mymiddle_iv, mymiddle_iv2, mymiddle_iv3, mymiddle_iv4;
        private View mymiddle_v, mymiddle_v2;
        private RadioGroup mymiddle_rg;

        public Mymiddle(View view) {
            mymiddle_tv = (TextView) view.findViewById(R.id.my_tv1);
            mymiddle_tv2 = (TextView) view.findViewById(R.id.textView2);
            mymiddle_tv3 = (TextView) view.findViewById(R.id.tv3);
            mymiddle_iv = (ImageView) view.findViewById(R.id.my_iv1);
            mymiddle_iv2 = (ImageView) view.findViewById(R.id.iv_rl);
            mymiddle_iv3 = (ImageView) view.findViewById(R.id.imageView);
            mymiddle_iv4 = (ImageView) view.findViewById(R.id.iv2_rl);
            mymiddle_v = view.findViewById(R.id.my_view);
            mymiddle_v2 = view.findViewById(R.id.my_tv1);
            mymiddle_rg = (RadioGroup) view.findViewById(R.id.my_rg);
        }
    }

    class MyThreeHolder {
        private ImageView my_lv_item_iv, my_lv_item_iv2;
        private TextView my_lv_tv;

        public MyThreeHolder(View view) {
            my_lv_item_iv = (ImageView) view.findViewById(R.id.my_lv_item_iv);
            my_lv_item_iv2 = (ImageView) view.findViewById(R.id.my_lv_item_iv2);
            my_lv_tv = (TextView) view.findViewById(R.id.my_lv_tv);
        }
    }

    //-------------------------------------------------------------------------------------------------------
    //重写4种方法
    @Override
    public int getCount() {
        return list == null ? 0 :list.size()+2;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int itemType = getItemViewType(position);
        MyTheme myTheme=null;
        Mymiddle mymiddle=null;
        MyThreeHolder myThreeHolder=null;
        if (convertView==null){
            switch(itemType){

                case TYPE_MY_ONE:
                    convertView= LayoutInflater.from(context).inflate(R.layout.my_item_head,parent,false);
                    myTheme = new MyTheme(convertView);
                    convertView.setTag(myTheme);
                    break;
                case TYPE_MY_TWO:
                    convertView= LayoutInflater.from(context).inflate(R.layout.my_item_one,parent,false);
                    mymiddle = new Mymiddle(convertView);
                    convertView.setTag(mymiddle);
                    break;
                case TYPE_MY_THREE:
                    convertView= LayoutInflater.from(context).inflate(R.layout.my_lv_item,parent,false);
                    myThreeHolder = new MyThreeHolder(convertView);
                    convertView.setTag(myThreeHolder);
                    break;
            }
        }else {
            switch(itemType){

                case TYPE_MY_ONE:
                    myTheme = (MyTheme) convertView.getTag();
                    break;
                case TYPE_MY_TWO:
                    mymiddle= (Mymiddle) convertView.getTag();
                    break;
                case TYPE_MY_THREE:
                    myThreeHolder= (MyThreeHolder) convertView.getTag();
                    break;
            }
        }


        switch (itemType){
                case TYPE_MY_ONE:
                    myTheme.rl_theme.setOnTouchListener(new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            Intent intent = new Intent(context,MyThemeActivity.class);
                            context.startActivity(intent);
                            return false;
                        }
                    });
                    break;
                case TYPE_MY_TWO:
                    mymiddle.mymiddle_rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(RadioGroup group, int checkedId) {
                            Toast.makeText(context,"sasassaas",Toast.LENGTH_LONG).show();

                        }
                    });
                    break;
                case TYPE_MY_THREE:

                        HashMap<String, Object> map = list.get(position-2);
                        int id = (int) map.get("id");
                        String title= (String) map.get("title");
                        myThreeHolder.my_lv_item_iv.setImageResource(id);
                        myThreeHolder. my_lv_tv.setText(title);

                    break;
        }

        return convertView;
    }
}
